package com.ncu.cloundeoffice.pojo;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import com.ncu.cloundeoffice.config.CustomAuthorityDeserializer;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TAdmin implements Serializable,  UserDetails{
    private int id;
    private String name;
    private String phone;
    private String telephone;
    private String address;

    @Getter(AccessLevel.NONE)
    private boolean enabled;
    private String username;
    private String password;
    private String userFace;
    private String remark;

    //用户角色
    private List<TRole> tRoles;


    private List<SimpleGrantedAuthority> authorities;

    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (int i = 0; i < tRoles.size(); i++) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(tRoles.get(i).getName());
            authorities.add(authority);
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled(){
        return enabled;
    }
}
